
using System;
using MvvmFx.Common.ViewModels.Behaviors.Observable;

namespace MvvmFx.Common.ViewModels
{
    /// <summary>
    /// The SelectableViewModel class inherits from <see cref="ObservableViewModel"/>
    /// and adds a public IsSelected property and SelectedChanged event.
    /// </summary>
    public abstract class SelectableViewModel : ObservableViewModel
    {
        #region Fields

        /// <summary>
        /// Indicate the isSelected state field.
        /// </summary>
        private Boolean isSelected = false;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="SelectableViewModel"/> class.
        /// </summary>
        protected SelectableViewModel()
            : base()
        {
        }

        #endregion

        #region Events

        /// <summary>
        /// The SelectedChanged event of type EventHandler. 
        /// </summary>
        public event EventHandler SelectedChanged;

        #endregion
        
        #region Properties
        
        /// <summary>
        /// Gets or sets the value of the <see cref="IsSelected"/> property.
        /// </summary>
        /// <value>If isSelected.</value>
        public Boolean IsSelected
        {
            get
            {
                return this.isSelected;
            }

            set
            {
                if (this.isSelected != value)
                {
                    this.isSelected = value;
                    this.OnSelectedChanged(new EventArgs());
                }
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Occurs when the <see cref="OnSelectedChanged"/> changes.
        /// </summary>
        /// <param name="e">The <see cref="EventArgs"/> event fired.</param>
        protected virtual void OnSelectedChanged(EventArgs e)
        {
            this.OnPropertyChanged("IsSelected");

            if (SelectedChanged != null)
            {
                SelectedChanged(this, e);
            }
        }

        #endregion
    }
}